package com.errol.generic.file.core.service;

import com.renjia.properties.UkerServerProperties;
import org.springframework.stereotype.Service;

/**
 * Created by Errol on 19/1/11.
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2020/10/21
 * @since v4.7.6
 */
@Service
public class FileTimingServiceImpl {

    private final String fileBasePath;
    private final FileInnerService fileInnerService;

    public FileTimingServiceImpl(UkerServerProperties ukerServerProperties, FileInnerService fileInnerService) {
        this.fileBasePath = ukerServerProperties.getFileBasePath();
        this.fileInnerService = fileInnerService;
    }

//    /**
//     * 每天 23:30 检查 Filepath 的二级目录，是否需要新建
//     */
//    @Scheduled(cron = "0 30 23 * * ?")
//    public void checkSecondaryFolderOfFilepath() {
//        for (Filepath filepath : Filepath.values()) {
//            fileInnerService.createSecondaryFolderOfFilepath(filepath, true);
//        }
//    }
//
//    /**
//     * 每天 02:20 清理 临时文件夹
//     * 清理 temp 目录下的文件；备份 昨天 的文件夹（需按天创建二级目录）；删除 7 天前的 备份文件夹
//     * 备份后的临时文件无法再访问，备份只是容错处理
//     */
//    @Scheduled(cron = "0 20 2 * * ?")
//    public void clearTempFile() throws IOException {
//        String tempPath = fileBasePath + Filepath.TEMP.getPath();
//        File tempDir = new File(tempPath);
//        if (tempDir.exists() && tempDir.isDirectory() && tempDir.list().length > 0) {
//            for (String filename : tempDir.list()) {
//                File file = new File(tempPath + filename);
//                if (!file.isDirectory()) {
//                    file.delete();
//                }
//            }
//        }
//
//        LocalDate yesterday = LocalDate.now().minusDays(1);
//        File tempYesterday = new File(tempPath + Frequency.DAILY.getSecondaryFolderPathOfFilepath(yesterday));
//        if (tempYesterday.exists()) {
//            tempYesterday.renameTo(new File(tempPath + yesterday.toString() + "_backup" + File.separator));
//        }
//
//        LocalDate daysAgo = yesterday.minusDays(3);
//        File fourDaysAgoDirectory = new File(tempPath + daysAgo.toString() + "_backup" + File.separator);
//        if (fourDaysAgoDirectory.exists() && fourDaysAgoDirectory.isDirectory()) {
//            FileUtils.deleteDirectory(fourDaysAgoDirectory);
//        }
//    }
//
//    /**
//     * 每天 04:00 清理二级目录的 空文件夹
//     */
//    @Scheduled(cron = "0 0 4 * * ?")
//    public void clearEmptyDirectory() {
//        LocalDate now = LocalDate.now();
//        for (Filepath filepath : Filepath.values()) {
//            Frequency frequency = filepath.getFrequency();
//            if (frequency != null) {
//                boolean needCheck = false;
//                switch (frequency) {
//                    case DAILY:
//                        needCheck = true;
//                        break;
//                    case WEEKLY:
//                        if (now.getDayOfWeek() == DayOfWeek.MONDAY) {
//                            needCheck = true;
//                        }
//                        break;
//                    case MONTHLY:
//                        if (now.getDayOfMonth() == 1) {
//                            needCheck = true;
//                        }
//                    default:
//                        break;
//                }
//                if (needCheck) {
//                    File file = new File(fileBasePath + filepath.getPath()
//                            + frequency.getSecondaryFolderPathOfFilepath(now.minusDays(1)));
//                    if (file.exists() && file.isDirectory() && file.list().length == 0) {
//                        file.delete();
//                    }
//                }
//            }
//        }
//    }
}
